package org.stvd.service.common.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.stvd.common.utils.ServiceResult;
import org.stvd.core.util.DateUtil;
import org.stvd.core.util.StringUtil;
import org.stvd.entities.common.AffixUpload;
import org.stvd.repository.common.AffixUploadDao;
import org.stvd.service.base.impl.BaseServiceImpl;
import org.stvd.service.common.AffixUploadService;

@Service("affixUploadService")
public class AffixUploadServiceImpl extends BaseServiceImpl<AffixUpload> implements AffixUploadService{

    @Resource(name="AffixUploadDao")
    private AffixUploadDao affixUploadDao;

    @Override
    public ServiceResult<Object> updateAffixUploadStatus(Long affixId, String status) {
        ServiceResult<Object> serviceResult = new ServiceResult<>();
        if(affixId == null || StringUtil.isEmpty(status)) {
            serviceResult.setParamError("参数信息异常");
            return serviceResult;
        }
        AffixUpload affixUpload = affixUploadDao.findByPk(AffixUpload.class, affixId);
        if(affixUpload==null) {
            serviceResult.setDataError("未查询到附件信息");
            return serviceResult;
        }
        if(status.equals(affixUpload.getStatus())) {
            serviceResult.setDataError("状态更新前后信息一致");
            return serviceResult;
        }
        affixUpload.setStatus(status);
        affixUpload.setModifyTime(DateUtil.getSystemDate());
        affixUploadDao.update(affixUpload);
        serviceResult.setMessage("操作成功");
        return serviceResult;
    }
    
    @Override
    public ServiceResult<Object> updateAffixStatusByAffixIds(String affixIds, String status) {
        ServiceResult<Object> serviceResult = new ServiceResult<>();
        if(StringUtil.isEmpty(affixIds) || StringUtil.isEmpty(status)) {
            serviceResult.setParamError("参数信息异常");
            return serviceResult;
        }
        int updateCount = affixUploadDao.updateAffixStatusByAffixIds(affixIds, status);
        serviceResult.setMessage("更新成功，本次更新数据"+updateCount+"条！");
        return serviceResult;
    }

    @Override
    public ServiceResult<Object> deleteAffixUpload(Date startDate, Date endDate) {
        ServiceResult<Object> serviceResult = new ServiceResult<>();
        if(endDate.before(startDate)) {
            serviceResult.setParamError("参数信息异常");
            return serviceResult;
        }
        affixUploadDao.deleteAffixUpload(startDate, endDate);
        serviceResult.setMessage("删除成功");
        return serviceResult;
    }

    @Override
    public Integer getNewOrderNo() {
        Integer orderNo = 1;
        AffixUpload affixUpload = affixUploadDao.getMaxOrderNoAffixUpload();
        if(affixUpload!=null) {
            orderNo = affixUpload.getOrderNo() + 1;
        }
        return orderNo;
    }
}
